<template>
  <a-card class="feedback-list--backend" ref="feedbackList" :bordered="false">
    <div class="header">
      <a-button
        v-if="$auth('SYSTEM_NOTICE.USER_FEEDBACK_EXPORT')"
        class="float-right"
        type="primary"
        :loading="loadExportCheck"
        @click="handleExport"
      >
        {{ $t('all.autotext1014') }}
      </a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      table-layout="fixed"
      :row-key="record => record.id"
      :columns="columns"
      :data="loadData"
    >
      <div slot="index" slot-scope="text, row, index">
        {{ getIndex(index) }}
      </div>
      <div slot="content" slot-scope="text" class="table__column--content">
        {{ text }}
      </div>
      <div slot="action" slot-scope="text, row">
        <a-button v-if="$auth('SYSTEM_NOTICE.USER_FEEDBACK_DETAIL')" type="link" @click="handleView(row)">{{ $t('all.autotext568') }}</a-button>
      </div>
    </s-table>
  </a-card>
</template>

<script>
import { apiGetFeedbackList, apiExportFeedbackList } from '@/api/feedback/feedback'
import { feedbackListBackendCol } from '@views/system-notice/config/column'
import { unreactive, getFileNameByResponse, exportBlob } from '@/utils/util'
import moment from 'moment'

export default {
  name: 'feedback-list--backend',
  data () {
    return {
      columns: unreactive(feedbackListBackendCol),
      loadExportCheck: false
    }
  },
  methods: {
    getIndex (index) {
      const { current, pageSize } = this.$refs.table.getPagination()
      return ((current - 1) * pageSize) + index + 1
    },
    loadData (param) {
      return apiGetFeedbackList(param).then(res => {
        if (res.content) {
          res.content = res.content.map(e => {
            e.createTime = moment(e.createTime).format('YYYY-MM-DD HH:mm:ss')
            return e
          })
        }
        return res
      })
    },
    handleExport () {
      this.loadExportCheck = true
      return apiExportFeedbackList().then(res => {
        const { headers } = res
        const fileName = getFileNameByResponse(headers)
        exportBlob(fileName, res.data)
      }).finally(() => {
        this.loadExportCheck = false
      })
    },
    handleView ({ id }) {
      this.$router.push({
        path: `/system-notice/details/${id}`
      })
    }
  }
}
</script>

<style scoped lang='less'>
.feedback-list--backend {
  .header {
    display: flow-root;
    margin-bottom: 10px;

    .float-right {
      float: right;
    }
  }
  .table__column--content {
    width: 500px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}

</style>
